package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussUserConsumorder;
import cn.pegasus.teamwork.pegasus.entity.BussUserFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussUserOrder;
import cn.pegasus.teamwork.pegasus.mapper.BussUserOrderMapper;
import cn.pegasus.teamwork.pegasus.service.BussPropertyInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUserConsumorderService;
import cn.pegasus.teamwork.pegasus.service.BussUserFunddetailsService;
import cn.pegasus.teamwork.pegasus.service.BussUserOrderService;
@Controller
@RequestMapping("/userOrder")
public class UserOrderController extends BaseManagerController<BussUserOrder, BussUserOrderMapper>{
	private static final Logger logger = LoggerFactory.getLogger(UserOrderController.class);
	
	@Autowired
	private BussUserFunddetailsService userFunddetailsService;
	@Autowired
	private BussUserConsumorderService userConsumorderService;
	@Autowired
	private BussUserOrderService userOrderService;
	@Autowired
	private BussPropertyInfoService bussPropertyInfoService;
	
	/**
	 * 查询用户资金明细列表（分页）
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("finance/userdetail-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussUserFunddetails> dicList = userFunddetailsService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询用户资金明细列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 用户消费明细
	 */
	@RequestMapping(value="/consumList")
	public ModelAndView consumList(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("finance/consum-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussUserConsumorder> dicList = userConsumorderService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询消费订单列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 查看用户消费明细详情
	 */
	@RequestMapping(value="/findConsumInfo")
	 public ModelAndView findConsumInfo(@RequestParam(value="id",required = false) Integer id){
		 ModelAndView view = new ModelAndView("finance/consum-info");
		 BussUserConsumorder info = userConsumorderService.findConsumInfo(id);
		 view.addObject("info", info);
		 return view;
	 }
	
	/**
	 * 查询洗车消费订单列表
	 */
	@RequestMapping(value="/orderList")
	public ModelAndView orderList(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("finance/order-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussUserOrder> dicList = userOrderService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询消费订单列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 查看洗车订单详情
	 */
	@RequestMapping(value="/findOrderInfo")
	 public ModelAndView findOrderInfo(@RequestParam(value="id",required = false) Long id){
		 ModelAndView view = new ModelAndView("finance/order-info");
		 BussUserOrder info = userOrderService.findRoderInfo(id);
		 view.addObject("info", info);
		 return view;
	 }
	 
	 /**
	  * 用户申请退款审核
	  * id  订单ID
	  * status  状态 0-不用审核；1-等待审核；2-通过；3-不通过
	  */
	@ResponseBody
	@RequestMapping(value="/examine")
	 public Map<String,Object> examine(@RequestParam(value="id",required = false) Long id,
			 @RequestParam(value="status",required = false) Integer status){
		 Map<String,Object> result = new HashMap<String,Object>();
		 try {
			 userOrderService.examine(id, status);
			 result = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
		 } catch (Exception e) {
            logger.error("退款审核异常", e);
            result = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
		 }
		 return result;
	 }
	
	/**
	 * 结算测试
	 */
	@ResponseBody
	@RequestMapping(value="/test")
	 public Map<String,Object> test(){
		 Map<String,Object> result = new HashMap<String,Object>();
		 try {
			 userOrderService.settlement();
		//	 bussPropertyInfoService.propertySettlement();
			 result = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
		 } catch (Exception e) {
            logger.error("退款审核异常", e);
            result = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
		 }
		 return result;
	 }
}
